package com.msb.serviceDriverUser.controller;

import com.msb.internalcommon.constant.DriverCarConstants;
import com.msb.internalcommon.dto.DriverUser;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.DriverUserExistsResponse;
import com.msb.internalcommon.reponese.OrderDriverResponse;
import com.msb.serviceDriverUser.service.DriverUserService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PathParam;

/**
 * @Auther: jiudianliu
 * @Date: 2023/6/29 - 06 - 29 - 16:19
 * @Description: com.msb.serviceDriverUser.controller
 * @version: 1.0
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private DriverUserService driverUserService ;

    /*
    * 新增司机
    * */
    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser){
        log.info(JSONObject.fromObject(driverUser).toString());
        return driverUserService.addDriverUser(driverUser);
    }


    /*
    * 修改司机信息
    * */
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser){
        log.info(JSONObject.fromObject(driverUser).toString());
        return driverUserService.updateDriverUser(driverUser) ;
    }

    /*
    *  根据条件查询司机
    * */

    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> getUser(@PathVariable("driverPhone") String driverPhone){
        ResponseResult<DriverUser> driverUserByPhone = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUser driverPhoneDB = driverUserByPhone.getData();
        DriverUserExistsResponse driverUserExistsResponse = new DriverUserExistsResponse();
        int ifExists = DriverCarConstants.DRIVER_EXISTS;
        if (driverPhoneDB == null){
            ifExists = DriverCarConstants.DRIVER_NOT_EXISTS ;
            driverUserExistsResponse.setDriverPhone(driverPhone);
            driverUserExistsResponse.setIfExists(ifExists);
        }else {
            driverUserExistsResponse.setDriverPhone(driverPhoneDB.getDriverPhone());
            driverUserExistsResponse.setIfExists(ifExists);
        }
        return ResponseResult.success(driverUserExistsResponse) ;
    }


    /**
     * 根据车辆Id查询订单需要的司机信息
     * @param carId
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId") Long carId){
       return driverUserService.getAvailableDriver(carId);
    }
}
